package edu.snake.implement.view;

import edu.snake.core.controller.GameEngine;
import edu.snake.implement.ConfigConstant;
import edu.snake.implement.view.control.ScoreIndicator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by SSS on 01.12.2014.
 */
public class ViewGeneral extends JFrame  {

    private GameEngine gameEngine;
    private JPanel topPanelWithButtons;

    private JButton startButton;
    private JButton pauseButton;
    private JButton endButton;

    private JLabel scoreTextLabel;
    private ScoreIndicator scoreCountTextLabel;

    private ViewArena arenaPanel;

    public ViewGeneral(GameEngine engine) {
        this.gameEngine = engine;
        initStatic();
    }

    void initStatic() {
        setResizable(false);
        setTitle("Snake");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.black);
        setFocusable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        topPanelWithButtons = new JPanel();

        initButtons();
        initScorePanel();
        initViewArena();


        initViewGeneralLayout();
        initTopPanelWithButtonLayout();

        pack();
    }



    private void initViewArena() {

        arenaPanel = new ViewArena(
                ConfigConstant.cellWidth * ConfigConstant.cellSizePixel
                , ConfigConstant.cellHeight * ConfigConstant.cellSizePixel
        );
        arenaPanel.setBackground(new Color(51, 51, 51));
        arenaPanel.setFocusable(true);
    }

    private void initScorePanel() {
        scoreTextLabel.setText("Score:");
        scoreCountTextLabel.setText("0000");
    }

    private void initButtons() {
        startButton = new JButton();
        pauseButton = new JButton();
        endButton = new JButton();

        startButton.setText("Start");
        pauseButton.setText("Pause");
        endButton.setText("End");

        pauseButton.setVisible(false);
        endButton.setVisible(false);



        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startButton.setVisible(false);
                pauseButton.setVisible(true);
                endButton.setVisible(true);
                gameEngine.startGame();
            }

        });

        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameEngine.pauseThreads();
            }
        });

        endButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pauseButton.setVisible(false);
                startButton.setVisible(true);
                endButton.setVisible(false);
                gameEngine.stopThreads();
            }
        });
    }



    private void initViewGeneralLayout() {
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(topPanelWithButtons
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
                        .addComponent(arenaPanel
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(topPanelWithButtons
                                        , javax.swing.GroupLayout.PREFERRED_SIZE
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(arenaPanel
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , Short.MAX_VALUE))
        );
    }

    private void initTopPanelWithButtonLayout() {
        javax.swing.GroupLayout topPanelWithButtonsLayout = new javax.swing.GroupLayout(topPanelWithButtons);
        topPanelWithButtons.setLayout(topPanelWithButtonsLayout);
        topPanelWithButtonsLayout.setHorizontalGroup(
                topPanelWithButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(topPanelWithButtonsLayout.createSequentialGroup()
                                        .addComponent(pauseButton
                                                , javax.swing.GroupLayout.PREFERRED_SIZE
                                                , 73
                                                , javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(startButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(endButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreTextLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreCountTextLabel)
                        )
        );
        topPanelWithButtonsLayout.setVerticalGroup(
                topPanelWithButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(topPanelWithButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(startButton)
                                .addComponent(endButton)
                                .addComponent(scoreTextLabel)
                                .addComponent(scoreCountTextLabel)
                                .addComponent(pauseButton))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(topPanelWithButtons
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
                        .addComponent(arenaPanel
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(topPanelWithButtons
                                        , javax.swing.GroupLayout.PREFERRED_SIZE
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(arenaPanel
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , Short.MAX_VALUE))
        );
    }
}
